﻿Public Class FrmInventory
    Private x_inventory As X_Inventory
    Private x_inventory_old As X_Inventory

    Private _parentObj As PO
    Private _parentForm As SuperForm

    Public Sub New(ByVal xInventory As X_Inventory, ByVal parentObj As PO, ByVal parentForm As SuperForm)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me._parentObj = parentObj
        Me._parentForm = parentForm

        Me.x_inventory = xInventory
        If (Not parentObj Is Nothing) Then
            Me.x_inventory.setValue(I_Inventory.ColumnName_MaTS, Me._parentObj.getValue(I_TSCD.ColumnName_MaSO))
            Me.x_inventory.setValue(I_Inventory.ColumnName_parentID, Me._parentObj.getValue(I_TSCD.ColumnName_id))
            Me.x_inventory.setValue(I_Inventory.ColumnName_FAType, Me._parentObj.getValue(I_TSCD.ColumnName_FAType))
            Me.x_inventory.setValue(I_Inventory.ColumnName_Status, CONSTANT.STATUS._DRAFTS)
        End If

        Me.x_inventory_old = Me.x_inventory

        ''Viet: Đổ dữ liệu lên Combobox
        Dim sqlWhere = "" & I_Council.ColumnName_Type & " = '" & COUNCIL_TYPE._INVENTORY & "'"
        Dim da As New DataTable
        da = New Query(New X_Council(-1), sqlWhere).dataTable
        Me.txtHDKiemKe.DisplayMember = I_Council.ColumnName_Display
        Me.txtHDKiemKe.ValueMember = I_Council.ColumnName_id
        Me.txtHDKiemKe.DataSource = da
        Me.txtHDKiemKe.SelectedIndex = 0

        ini()
    End Sub

    Public Function ini() As Boolean

        Me.txtNguyenGia.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_NguyenGia)
        Me.txtGiaTriHaoMon.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_GiaTriHaoMon)
        Me.txtGiaTriConLai.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_GiaTriConLai)
        Me.txtDiaDiem.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_DiaDiem)
        Me.dtpThoiGian.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_ThoiGian)
        Me.txtHDKiemKe.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_HDKiemKe)
        Me.txtTinhTrangKT.Text = Me.x_inventory_old.getValue(I_Inventory.ColumnName_TinhTrangKiThuat)

    End Function


    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ini()
    End Sub


    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.x_inventory.setValue(I_Inventory.ColumnName_NguyenGia, Me.txtNguyenGia.Text)
        Me.x_inventory.setValue(I_Inventory.ColumnName_GiaTriHaoMon, Me.txtGiaTriHaoMon.Text)
        Me.x_inventory.setValue(I_Inventory.ColumnName_GiaTriConLai, Me.txtGiaTriConLai.Text)
        Me.x_inventory.setValue(I_Inventory.ColumnName_DiaDiem, Me.txtDiaDiem.Text)
        Me.x_inventory.setValue(I_Inventory.ColumnName_ThoiGian, Me.dtpThoiGian.Text)
        Me.x_inventory.setValue(I_Inventory.ColumnName_HDKiemKe, Me.txtHDKiemKe.SelectedValue)
        Me.x_inventory.setValue(I_Inventory.ColumnName_TinhTrangKiThuat, Me.txtTinhTrangKT.Text)

        Me.x_inventory.save(True)

        Me._parentForm.loadInventory()
        Me._parentForm.specifyCorrespondingInventory()
        Me.Close()
    End Sub
End Class